<template>
  <ContentWrap>
    <fromTitle title="承检机构资质及人员信息" :hasPermi="'qis:organization-qualification:query'"  :queryParams="queryParams" @search="getList" @reset="resetQuery"/> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item label="机构名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" label-width="150px" prop="name">
        <el-input
          v-model="queryParams.unifiedSocialCreditIdentifier"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="info" v-hasPermi="['qis:organization-staff:export']"  @click="handleExport">导出</el-button>

    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="机构名称" align="center" prop="name">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row.id)">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="机构地址" align="center" prop="address" />
      <el-table-column label="法人单位名称" align="center" prop="legalPersonCompany" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditIdentifier" />
      <el-table-column label="联系方式" align="center" prop="receivePhone" />
      <el-table-column label="机构资质" align="center" prop="organizationQualification" >
            <template #default="scope">
              <!-- {{ scope.row.organizationQualification.replace('；',';')}} -->
              {{scope.row.organizationQualification}}
              </template>
        </el-table-column>
      <el-table-column label="机构人数" align="center" prop="organizationStaffNum" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrganizationStaffForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrganizationStaffApi, OrganizationStaffVO } from '@/api/qis/organizationstaff'
import OrganizationStaffForm from './OrganizationStaffForm.vue'
import { OrganizationQualificationApi } from '@/api/qis/organizationqualification'

/** 质检平台-机构人员 列表 */
defineOptions({ name: 'OrganizationStaff' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrganizationStaffVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
let queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  unifiedSocialCreditIdentifier: undefined,
  name: undefined,

})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
  
    
    const data = await OrganizationQualificationApi.getOrganizationQualificationPage1(queryParams)
    console.log(data,'data')


        list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const arrayList = ref([])
const handleSelect = (row ) => {
  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(item => item !== row.id)
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams = Object.assign(queryParams, {
    pageNo: 1,
  pageSize: 10,
  unifiedSocialCreditIdentifier: undefined,
  name: undefined,
  })
  
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrganizationStaffApi.deleteOrganizationStaff(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  if(total.value > 10000) return message.error('当前数据过大，请筛选数据后导出')
  try {
if(arrayList.value.length > 0){
  const idsArr = arrayList.value.join(';')   
      // 导出的二次确认
      loading.value = true
      await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrganizationQualificationApi.exportOrganizationQualification({ids:idsArr,...queryParams})
    download.excel(data, '质检平台-机构人员.xls')
}else {
  await message.exportConfirm()
    // 发起导出
    loading.value = true
    exportLoading.value = true
    const data = await OrganizationQualificationApi.exportOrganizationQualification(queryParams)
    download.excel(data, '质检平台-机构人员.xls')
}

  } catch {
  } finally {
    exportLoading.value = false
    loading.value = false
  }
}

/** 查看详情操作 */
const router = useRouter()
const handleDetail = (id: any) => {
  router.push({
    path: '/inspection-info-management/organizationstaff/detail',
    query: { id: id }
  })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>